package com.example.demo.controller;



import com.example.demo.entity.Root;
import com.example.demo.entity.Student;
import com.example.demo.entity.Teacher;
import com.example.demo.mapper.RootMapper;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.mapper.TeacherMapper;
import com.example.demo.utils.ResultUtil;
import org.apache.tomcat.util.http.parser.Cookie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.dto.ResultJson;
import com.example.demo.entity.User;
import com.example.demo.mapper.User_Mapper;
import com.example.demo.utils.DateUtils;
import com.example.demo.utils.MD5Utils;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("user")
public class UserController extends BasicController<User> {
	@Autowired
	User_Mapper mapper;
	@Autowired(required = false)
	private StudentMapper studentMapper;
	@Autowired
	private TeacherMapper teacherMapper;
	@Autowired
	private RootMapper rootMapper;

	
	@Override
	public ResultJson insert(User u) {
		u.setPass(MD5Utils.MD5("123"));
		u.setCreatedate(DateUtils.getNowTime());
		return super.insert(u);
	}

	@RequestMapping("login")
	public ResultUtil login(HttpSession session, String userName, String password, String role) {
		if ("root".equals(role)){
			Root root = rootMapper.selectByUserName(userName, password);
			if (root!=null){
				session.setAttribute("id",root.getId());
				return new ResultUtil(200,"成功",null,null);
			}
		}
		else if ("student".equals(role)){
			Student student = studentMapper.selectByUserName(userName, password);
			if (student != null){
				session.setAttribute("id",student.getId());
				return new ResultUtil(200,"成功",null,null);
			}
		}else if ("teacher".equals(role)){
			Teacher teacher = teacherMapper.selectByUserName(userName, password);
			if (teacher != null){
				session.setAttribute("id",teacher.getId());
				return new ResultUtil(200,"成功",null,null);
			}
		}
		return new ResultUtil(500,"失败",null,null);
	}
}
